package com.faf.ui;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.faf.bean.User;
import com.faf.dao.impl.UserDaoImpl;
import com.faf.service.impl.UserServiceImpl;
import com.faf.tools.PHOUtils;
import com.faf.tools.RSAUtils;

/**
 * Servlet implementation class UserLogin
 */
@WebServlet("/UserLogin")
public class UserLogin extends HttpServlet {
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置session
		   HttpSession session = request.getSession();
		//设置中文格式
			response.setContentType("text/html;charset=utf-8"); 
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("UTF-8"); // 先指定输出流的编码
		//读取login.jsp中输入的账号/密码/验证码
			String user_number = request.getParameter("user_number");
			String user_pwd = request.getParameter("user_pwd");
			String rand = request.getParameter("rand");
		//把是否需要验证码卡关放入session
			String rand_check = null ;
			if(session.getAttribute("rand_check")==null){
				session.setAttribute("rand_check", "ok");
			}
			else{
				rand_check = (String) session.getAttribute("rand_check");
			}
			
			int error_password = 0;
			if(session.getAttribute("error_password")==null){
				error_password=0;
			}
			else{
				error_password = Integer.parseInt(session.getAttribute("error_password").toString());
			}
			String user_Phone = user_number;
		//查询账号对应的用户各信息
			int user_id = 0;
			String user_password = null;
			String user_realName = null ;
			String user_account = null ;
			String user_sex = null ;
			int user_age = 0 ;
			String user_homeTown = null ;
			String user_address = null ;
			String user_phone = null ;
			String user_signDate = null;
			UserDaoImpl udi = new UserDaoImpl()   ;
			try {
				//如果是手机号
				if(!PHOUtils.checkNickName(user_Phone)){
					//通过手机号获取他的用户名
					User u = udi.getUserByPhone(user_Phone);
					//相当于获取到了账号
					user_number = u.getUserNumber();
					System.out.println("获取到的账号为"+user_number);
				}
				
				//将该用户的所有信息打包收集
				User user = udi.getUserByNumber(user_number);
				 user_realName = user.getUserName();
				 user_password = (user.getUserPassword());
				 if(user_password!=null)
				 user_password = RSAUtils.privateDecrypt(user_password);//这是一次加密操作
				 user_account =  user.getUserNumber();
				 user_sex = user.getUserSex();
				 System.out.println("该用户性别"+user_sex);
				 user_age = user.getUserAge();
				 user_homeTown = user.getUserHometown();
				 user_address =  user.getUserAddress();
				 user_phone = user.getUserPhone();
				 user_signDate = user.getUserSignDate();
				 user_id = user.getUserId();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			try {
				//如果登陆成功进入个人信息页面
				int check = new UserServiceImpl().check(user_number ,user_pwd); 
				String json = "";
				if(check==1)
				{
					//定义用户的所有信息
					String name = null;
					String sex = null;
					String password = null;
					String account = null ;
					int age = 0 ;
					String hometown = null ;
					String address = null ;
					String phone = null ;
					String signDate = null;
					//将所有的信息进行编码
					if(user_account!=null)
						account=java.net.URLEncoder.encode(user_account, "UTF-8");
					if(user_password!=null)
						password=java.net.URLEncoder.encode(user_password, "UTF-8");
					if(user_realName!=null)
						name=java.net.URLEncoder.encode(user_realName, "UTF-8");
					if(user_sex!=null)
						sex =java.net.URLEncoder.encode(user_sex, "UTF-8");
					if(user_homeTown!=null)
						hometown=java.net.URLEncoder.encode(user_homeTown, "UTF-8");
					if(user_address!=null)
						address=java.net.URLEncoder.encode(user_address, "UTF-8");
					if(user_phone!=null)
						phone=java.net.URLEncoder.encode(user_phone, "UTF-8");
					if(user_signDate!=null)
						signDate=java.net.URLEncoder.encode(user_signDate, "UTF-8");
					if(user_age!=0)
						age = user_age;
					//将所有信息存入session
					session.setAttribute("name",name);
					session.setAttribute("phone",phone);
					session.setAttribute("password",password);
					session.setAttribute("account",account);
					session.setAttribute("sex",sex);
					System.out.println(sex);
					session.setAttribute("hometown",hometown);
					session.setAttribute("address",address);
					session.setAttribute("signDate",signDate);
					session.setAttribute("age",age);
					session.setAttribute("id", user_id);
					session.setAttribute("global_cart", new HashMap<Integer, Integer>());
					
					if(session.getAttribute("rand_check").equals("ok")){
						json = "{\"code\":"+1+"}";
						System.out.println("登陆成功");
					}
					else{
						if(rand.equals(session.getAttribute("rand"))){
							json = "{\"code\":"+1+"}";
							System.out.println("验证码验证通过");
						}
						else{
							json = "{\"code\":"+3+"}";
							System.out.println("验证码验证不通过");
						}
					}
					response.getWriter().print(json);
				}
				else if(check==-1){
					//ajax传值
					 json = "{\"code\":"+-1+"}";
					 System.out.println("用户不存在");
					 response.getWriter().print(json);

				}
				else if(check==2){
					//ajax传值
					 json = "{\"code\":"+2+"}";
					 System.out.println("请输入账号");
					 response.getWriter().print(json);
				}
				else if(check==-2){
					//ajax传值
					 json = "{\"code\":"+-2+"}";
					 System.out.println("请输入密码");
					 response.getWriter().print(json);
				}
				
				else{
					//ajax传值
					error_password+=1;
					
					 json = "{\"code\":"+0+"}";
					
					 System.out.println("账号或密码不正确");
					 System.out.println("输错"+error_password+"次");
					 
					 session.setAttribute("error_password", ""+error_password);
					 if(error_password >= 3){
						 System.out.println("输错3次了！");
						 //做个标记,将rand置空
						 session.setAttribute("rand_check", "no");
						 json = "{\"code\":"+4+"}";
					 }
					 response.getWriter().print(json);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
	}

	
}
